﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using RBS.Common;
using System.Diagnostics;
using System.Xml;

namespace SW.Common
{
    [Serializable]
    public class ApkEntity
    {
        public String FileName { get; set; }
        public long FileSize { get; set; }

        public String PrefixName { get; set; }
        public String VersionCode { get; set; }
        public String PackageName { get; set; }
        public String VersionName { get; set; }

        public String ApkPath { get; set; }
    }

    public class ApkCommon
    {
        private readonly string UploadApkPath = ConfigHelper.GetConfigString("UploadApkPath");

        ApkEntity apkEntity = null;
        public ApkEntity InitApk(String sourceFile)
        {
            apkEntity = new ApkEntity();
            CopyTo(sourceFile);

           
            
            String apkParentPath = String.Format(@"{0}\{1}", UploadApkPath, apkEntity.PrefixName);

            GetFileInfoVersion(String.Format(@"{0}\AndroidManifest.xml", apkParentPath));

            List<String> fileInfoList = new List<string>();

            fileInfoList.Add(String.Format(@"{0}\res\drawable\icon.png", apkParentPath));
            fileInfoList.Add(String.Format(@"{0}\res\drawable-hdpi\icon.png", apkParentPath));
            fileInfoList.Add(String.Format(@"{0}\res\drawable-mdpi\icon.png", apkParentPath));
            fileInfoList.Add(String.Format(@"{0}\res\drawable-ldpi\icon.png", apkParentPath));

            GetIcon(fileInfoList);

            return apkEntity;
        }

        private void CopyTo(String sourceFile)
        {
            try
            {
                string path = sourceFile;

                FileInfo fi = new FileInfo(path);
                if (fi.Exists)
                {

                    apkEntity.FileName = fi.Name;
                    apkEntity.FileSize = fi.Length;
                    string fileExt = apkEntity.FileName.Substring(apkEntity.FileName.LastIndexOf(".")).ToLower();
                    apkEntity.PrefixName = Guid.NewGuid().ToString();

                    apkEntity.FileName = apkEntity.PrefixName + fileExt;
                    string path2 = UploadApkPath + @"\" + apkEntity.FileName;
                    fi.CopyTo(path2);//将指定路径文件夹中的文件拷贝到该文件夹中,并将该文件改名
                }
            }
            catch
            {
                //出错信息
            }


        }

        private String GetIcon(List<String> fileInfoList)
        {
            FileInfo fileInfo = null;
            foreach (var item in fileInfoList)
            {
                fileInfo = new FileInfo(item);

                if (!fileInfo.Exists)
                {
                    continue;
                }
                apkEntity.ApkPath = item;
                return apkEntity.ApkPath;
            }

            return null;
        }

        private void GetFileInfoVersion(String fileName)
        {
            FileInfo fileInfo = new FileInfo(fileName);

            if (!fileInfo.Exists)
            {
                return;
            }

            XmlDocument xml = new XmlDocument();

            xml.Load(fileName);

            XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xml.NameTable);
            namespaceManager.AddNamespace("android", "http://schemas.android.com/apk/res/android");


            XmlNode xmlNode = xml.SelectSingleNode("/manifest/@android:versionName", namespaceManager);

            if (xmlNode != null)
            {
                apkEntity.VersionName = xmlNode.Value;
            }

            xmlNode = xml.SelectSingleNode("/manifest/@android:versionCode", namespaceManager);

            if (xmlNode != null)
            {
                apkEntity.VersionCode = xmlNode.Value;
            }

            xmlNode = xml.SelectSingleNode("/manifest/@package");

            if (xmlNode != null)
            {
                apkEntity.PackageName = xmlNode.Value;
            }

        }

       
    }
}
